﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using IDE;
using CommonClasses.Logger;
using PinsDescriptionEditor;
using Descriptions_PinsDescription.Model;

namespace PinsDescriptionEditor
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ILoggingManagement loggingManagment;
            ILogger logger = null;

            loggingManagment = LoggerManager.CreateOnce(".\\LoggerManager.ih.config", ".\\logs");
            logger = loggingManagment.GetLogger("PortEditorLogger");

            var ecoSpace = new IDEEcoSpace(logger);
            ecoSpace.Active = true;
            CreatePortCAST1(ecoSpace);
            //new PinsDescriptionLoader.PinsDescriptionLoader(ecoSpace).SavePortToFile(@".\cfg\TADiOM.ihport");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new PinsDescriptionEditor(ecoSpace));
        }

        private static void CreatePortCAST1(IDEEcoSpace ecoSpace)
        {
            PinsDescriptionTable pinsDescriptionTable = new PinsDescriptionTable(ecoSpace);
            #region MP11
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 0, RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 1, RealPin = 11, Direction = true, Descriptor = "KE1" });     //UpperHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 34, RealPin = 6, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 2, RealPin = 13, Direction = true, Descriptor = "KE1" });     //UpperHand.Back
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 35, RealPin = 24, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Back

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 3, RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.Arm.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 4, RealPin = 14, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.RotateRight
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 36, RealPin = 7, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Arm.RotateRight
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 5, RealPin = 16, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.RotateLeft
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 37, RealPin = 10, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Arm.RotateLeft

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 6,  RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.Arm.Fingers.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 7,  RealPin =  2, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.Fingers.Close/Open

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 8, RealPin = -1, Direction = false, Descriptor = "State" });   //BottomHand.StatePin
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 9, RealPin = 12, Direction = true, Descriptor = "KE1" });     //BottomHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 38, RealPin = 21, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 10, RealPin = 15, Direction = true, Descriptor = "KE1" });    //BottomHand.Back
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 39, RealPin = 23, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Back

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 11, RealPin = -1, Direction = false, Descriptor = "State" });  //BottomHand.Arm.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 12, RealPin =  6, Direction = true, Descriptor = "KE2" });    //BottomHand.Arm.Close/Open
            
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 13, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 14, RealPin =  4, Direction = true, Descriptor = "KE1" });    //Table.Up/Down
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 40, RealPin = 8, Direction = false, Descriptor = "KE1" });     //sensor Table.Up
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 41, RealPin = 9, Direction = false, Descriptor = "KE1" });     //sensor Table.Down

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 15, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.Rotation.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 16, RealPin =  1, Direction = true, Descriptor = "KE1" });    //Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 42, RealPin = 19, Direction = false, Descriptor = "KE1" });     //sensor Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 17, RealPin =  5, Direction = true, Descriptor = "KE1" });    //Table.Rotation.Left
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 43, RealPin = 20, Direction = false, Descriptor = "KE1" });     //sensor Table.Rotation.Left
            #endregion
            #region mp9
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 18, RealPin = -1, Direction = false, Descriptor = "State" });  //Hand.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 19, RealPin = 1, Direction = true, Descriptor = "KE2" });     //Hand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 44, RealPin = 17, Direction = false, Descriptor = "KE2" });     //sensor Hand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 20, RealPin = 9, Direction = true, Descriptor = "KE2" });     //Hand.Back
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 45, RealPin = 13, Direction = false, Descriptor = "KE2" });     //sensor Hand.Back

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 21, RealPin = -1, Direction = false, Descriptor = "State" });  //Hand.Arm.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 22, RealPin =  4, Direction = true, Descriptor = "KE2" });    //Hand.Arm.Close/Open
            
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 23, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 24, RealPin =  7, Direction = true, Descriptor = "KE2" });    //Table.Up/Down
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 46, RealPin = 16, Direction = false, Descriptor = "KE2" });     //sensor Table.Up
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 47, RealPin = 14, Direction = false, Descriptor = "KE2" });     //sensor Table.Down

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 25, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.Rotation.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 26, RealPin =  3, Direction = true, Descriptor = "KE2" });    //Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 48, RealPin = 12, Direction = false, Descriptor = "KE2" });     //sensor Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 27, RealPin = 17, Direction = true, Descriptor = "KE1" });    //Table.Rotation.Left
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 49, RealPin = 15, Direction = false, Descriptor = "KE2" });     //sensor Table.Rotation.Left
            #endregion
            #region Piston
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 28, RealPin = -1, Direction = false, Descriptor = "State" });   //Lift.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 29, RealPin =  2, Direction = true, Descriptor = "KE2" });    //Lift.Up
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 50, RealPin = 18, Direction = false, Descriptor = "KE2" });     //sensor Lift.Up
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 30, RealPin =  5, Direction = true, Descriptor = "KE2" });    //Lift.Down
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 51, RealPin = 23, Direction = false, Descriptor = "KE2" });     //sensor Lift.Down
            #endregion
            #region Modelling
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 31, RealPin = -1, Direction = false, Descriptor = "State" });   //Modelling.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 32, RealPin = -1, Direction = true, Descriptor = "" });//Modelling.SetCheckpoint
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 33, RealPin = -1, Direction = true, Descriptor = "" });//Modelling.Rollback
            #endregion
            #region Etalons
            pinsDescriptionTable.Etalons.Add(new EtalonDescription (ecoSpace) { Descriptor = "KE1", Etalon = "#IO,000001111100000001111111\r" });
            pinsDescriptionTable.Etalons.Add(new EtalonDescription(ecoSpace) { Descriptor = "KE2", Etalon = "#IO,000000000011111111111111\r" });
            #endregion
        }

        private static void CreatePortCAST2(IDEEcoSpace ecoSpace)
        {
            PinsDescriptionTable pinsDescriptionTable = new PinsDescriptionTable(ecoSpace);

            #region MP11
            
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 0, RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 1, RealPin = 19, Direction = true, Descriptor = "KE1" });     //UpperHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 50, RealPin = 22, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 2, RealPin = 7, Direction = true, Descriptor = "KE1" });     //UpperHand.Back
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 51, RealPin = 17, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Back

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 3, RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.Arm.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 4, RealPin = 4, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.MoveRight
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 52, RealPin = 24, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Arm.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 5, RealPin = 8, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.MoveLeft
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 53, RealPin = 13, Direction = false, Descriptor = "KE1" });     //sensor UpperHand.Arm.Left

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 6, RealPin = -1, Direction = false, Descriptor = "State" });   //UpperHand.Arm.Fingers.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 7, RealPin = 3, Direction = true, Descriptor = "KE1" });     //UpperHand.Arm.Fingers.Close/Open

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 8, RealPin = -1, Direction = false, Descriptor = "State" });   //BottomHand.StatePin
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 9, RealPin = 5, Direction = true, Descriptor = "KE1" });     //BottomHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 54, RealPin = 12, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Forward
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 10, RealPin = 18, Direction = true, Descriptor = "KE1" });    //BottomHand.Back
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 55, RealPin = 23, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Back

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 11, RealPin = -1, Direction = false, Descriptor = "State" });   //BottomHand.Arm.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 12, RealPin = 2, Direction = true, Descriptor = "KE1" });     //BottomHand.Arm.RotateLeft
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 56, RealPin = 16, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Arm.Left
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 13, RealPin = 1, Direction = true, Descriptor = "KE1" });     //BottomHand.Arm.RotateRight
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 57, RealPin = 14, Direction = false, Descriptor = "KE1" });     //sensor BottomHand.Arm.Right

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 14, RealPin = -1, Direction = false, Descriptor = "State" });  //BottomHand.Arm.Fingers.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 15, RealPin = 3, Direction = true, Descriptor = "KE1" });    //BottomHand.Arm.Fingers.Close/Open

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 16, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 17, RealPin = 9, Direction = true, Descriptor = "KE1" });    //Table.Up/Down
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 58, RealPin = 21, Direction = false, Descriptor = "KE1" });     //sensor Table.Up
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 59, RealPin = 20, Direction = false, Descriptor = "KE1" });     //sensor Table.Down

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 18, RealPin = -1, Direction = false, Descriptor = "State" });  //Table.Rotation.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 19, RealPin = 10, Direction = true, Descriptor = "KE1" });    //Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 60, RealPin = 15, Direction = false, Descriptor = "KE1" });     //sensor Table.Rotation.Right
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 20, RealPin = 6, Direction = true, Descriptor = "KE1" });    //Table.Rotation.Left
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 61, RealPin = 11, Direction = false, Descriptor = "KE1" });     //sensor Table.Rotation.Left
            
            #endregion

            #region Chill

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 21, RealPin = -1, Direction = false, Descriptor = "State" });  //Chill.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 22, RealPin = 1, Direction = true, Descriptor = "KE2" });     //Chill.Close
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 62, RealPin = 11, Direction = false, Descriptor = "KE2" });     //sensor Chill.Close
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 23, RealPin = 2, Direction = true, Descriptor = "KE2" });     //Chill.Open
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 63, RealPin = 12, Direction = false, Descriptor = "KE2" });     //sensor Chill.Open
            
            #endregion

            #region Stove

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 24, RealPin = -1, Direction = false, Descriptor = "State" });   //Stove.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 25, RealPin = 3, Direction = true, Descriptor = "KE2" });    //Stove.On
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 64, RealPin = 13, Direction = false, Descriptor = "KE2" });     //sensor Stove.On
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 26, RealPin = 4, Direction = true, Descriptor = "KE2" });    //Stove.Off
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 65, RealPin = 14, Direction = false, Descriptor = "KE2" });     //sensor Stove.Off

            #endregion

            #region Modelling

            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 27, RealPin = -1, Direction = false, Descriptor = "State" });   //Modelling.State
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 28, RealPin = -1, Direction = true, Descriptor = "" });//Modelling.SetCheckpoint
            pinsDescriptionTable.PinTable.Add(new PinDescription(ecoSpace) { VirtualPin = 29, RealPin = -1, Direction = true, Descriptor = "" });//Modelling.Rollback

            #endregion

            #region Etalons

            pinsDescriptionTable.Etalons.Add(new EtalonDescription(ecoSpace) { Descriptor = "KE1", Etalon = "#IO,000000000011111110011111\r" });

            #endregion
        }
    }
}
